package com.buddies.lottery.base;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.buddies.lottery.R;
import com.buddies.lottery.widget.OnTabClickListener;

import java.util.ArrayList;

public abstract class BasePagerFragment<P extends IPresenter> extends BaseFragment implements OnTabClickListener, TabLayout.OnTabSelectedListener {

    protected ArrayList<String> mTabTexts = new ArrayList<>();
    protected ArrayList<Fragment> mFragments = new ArrayList<>();

    protected ViewPager mViewPager;
    protected PagerAdapter mPagerAdapter;
    protected TabLayout mTabLayout;
    protected ImageView mAction;

    protected boolean isInit;
    protected boolean isLazy = true;

    public int getLayoutId() {
        return R.layout.fragment_pager;
    }

    @Override
    public void initView() {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        mAction = (ImageView) findViewById(R.id.iv_action);
    }

    @Override
    public void initData() {
        lazyLoad();
    }

    protected void lazyLoad() {
        if (isInit || mTabLayout == null || (isLazy && !getUserVisibleHint())) return;

        isInit = true;

        initTabTextAndFragment();
        mPagerAdapter = new PagerAdapter(getActivity(), getChildFragmentManager(), mTabTexts, mFragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(mTabTexts.size());
        mTabLayout.setupWithViewPager(mViewPager);

        updateTabTextAndFragment();

        mTabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        lazyLoad();
        Fragment fragment = getCurrentTabFragment();
        if (fragment != null) {
            fragment.setUserVisibleHint(isVisibleToUser);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Fragment fragment = getCurrentTabFragment();
        if (fragment != null) {
            fragment.onHiddenChanged(hidden);
        }
    }

    protected abstract void initTabTextAndFragment();

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        onTabClick();
    }

    private Fragment getCurrentTabFragment() {
        if (mPagerAdapter != null) {
            return (Fragment) mPagerAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());
        }
        return null;
    }

    public void setCurrentTab(int item) {
        mViewPager.setCurrentItem(item);
    }

    @Override
    public void onTabClick() {
        Fragment fragment = getCurrentTabFragment();
        if (fragment instanceof OnTabClickListener) {
            ((OnTabClickListener) fragment).onTabClick();
        }
    }

    public void updateTabTextAndFragment() {
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            mTabLayout.getTabAt(i).setCustomView(mPagerAdapter.getTabView(i));
        }
    }

    public void updateTabNewTips(int index, int visibility) {
        TabLayout.Tab tab = mTabLayout != null ? mTabLayout.getTabAt(index) : null;
        View view = tab != null ? tab.getCustomView() : null;
        View new_tips = view != null ? view.findViewById(R.id.new_tips) : null;
        if (new_tips != null) {
            new_tips.setVisibility(visibility);
        }
    }
}
